/*******************************************************************************
 * Copyright 2011, 2012 Chris Banes.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *******************************************************************************/
package com.huewu.pla.pinterest;

import opens.components.http.ImageRequest;
import opens.components.http.core.HttpRequest;
import opens.components.http.core.RequestQueue;
import opens.components.view.RemoteImageView;
import opens.components.view.RemoteImageViewListener;
import uk.co.senab.photoview.PhotoViewAttacher;
import android.app.Activity;
import android.content.Intent;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageView.ScaleType;
import android.widget.ProgressBar;
import android.widget.Toast;

import com.flurry.android.FlurryAgent;
import com.idreems.zakerlike.MyApplication;
import com.idreems.zakerlike.R;
import com.idreems.zakerlike.util.Util;

public class PhotoViewActivity extends Activity implements RemoteImageViewListener {

    public static final String kURL = "url";
    public static final String kPlaceholderImage = "placeholderImage";

    private static final String kDefaultURL = "http://ww3.sinaimg.cn/large/62f87eb4jw1e171e7rpwrj.jpg";
    private PhotoViewAttacher mAttacher;
    private String mImageUrl = kDefaultURL;
    private ProgressBar mProgressBar;
    private Button mSaveButton;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        Bitmap placeholder = null;
        Intent intent = getIntent();
        if (null != intent) {
            mImageUrl = intent.getStringExtra(kURL);
            if (TextUtils.isEmpty(mImageUrl)) {
                mImageUrl = kDefaultURL;
            }
            placeholder = (Bitmap) intent.getParcelableExtra(kPlaceholderImage);
        }
        setContentView(R.layout.photo_view);
        ProgressBar progressBar = (ProgressBar) findViewById(R.id.progessbar);
        progressBar.setVisibility(View.VISIBLE);
        RemoteImageView remoteImageView = (RemoteImageView) findViewById(R.id.photo);
        if (null != placeholder) {
            remoteImageView.setImageBitmap(placeholder);
        }
        remoteImageView.setRemoteImageViewListener(this);
        remoteImageView.loadFromUrl(mImageUrl, MyApplication.getCache());
        mProgressBar = (ProgressBar) findViewById(R.id.progessbar);
        mSaveButton = (Button) findViewById(R.id.save);
        mSaveButton.setVisibility(View.GONE);
        if (null != mSaveButton) {
            mSaveButton.setOnClickListener(new OnClickListener() {
                @Override
                public void onClick(View v) {
                    mProgressBar.setVisibility(View.VISIBLE);
                    RequestQueue.instance().push(new Runnable() {

                        @Override
                        public void run() {
                            ImageRequest request = new ImageRequest();
                            request.get(mImageUrl);
                            final String fileName = request.getExternalCacheFileName(getApplicationContext());
                            Util.save(mImageUrl, fileName);
                            runOnUiThread(new Runnable() {

                                @Override
                                public void run() {
                                    FlurryAgent.logEvent(Util.kFlurryEventOnLargeImageSaved);
                                    mProgressBar.setVisibility(View.GONE);
                                    Toast.makeText(getApplicationContext(), fileName, Toast.LENGTH_SHORT).show();
                                }
                            });
                        }
                    });
                }
            });
        }
    }

    @Override
    public void onImageRequestComplete(Bitmap bitmap) {
        mProgressBar.setVisibility(View.GONE);
        if (null != bitmap) {
            mSaveButton.setVisibility(View.VISIBLE);
            RemoteImageView remoteImageView = (RemoteImageView) findViewById(R.id.photo);
            mAttacher = new PhotoViewAttacher(remoteImageView);
            mAttacher.setScaleType(ScaleType.CENTER_INSIDE);
        }
    }

    @Override
    public void onTimelineRequestError(HttpRequest request) {
        mProgressBar.setVisibility(View.GONE);
        Toast.makeText(getApplicationContext(), "请检查网络是否可用", Toast.LENGTH_SHORT).show();
    }
}
